package com.skyworth.web.admin.store.service.impl;

import com.google.common.collect.Lists;
import com.google.gson.JsonObject;
import com.skyworth.web.admin.store.form.StoreForm;
import com.skyworth.web.admin.store.model.StoreModel;
import com.skyworth.web.admin.store.repository.StoreRepository;
import com.skyworth.web.admin.store.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by skyworth on 2016/8/19.
 */
@Service
public class StoreServiceImpl implements StoreService {

	@Autowired
	private StoreRepository storeRepository;

	@Override
	public StoreModel addStore(StoreForm storeForm) {
		StoreModel storeModel = new StoreModel();
		storeModel.name = storeForm.name;
		storeModel.ownerId = storeForm.ownerId;
		storeModel.description = storeForm.description;
		storeModel.logoId = storeForm.logoId;
		storeRepository.save(storeModel);

		return storeModel;
	}

	@Override
	public StoreModel updateStore(StoreForm storeForm) {
		StoreModel storeModel = storeRepository.findOne(storeForm.id);
		storeModel.name = storeForm.name;
		storeModel.logoId = storeForm.logoId;
		storeModel.ownerId = storeForm.ownerId;
		storeModel.description = storeForm.description;
		storeRepository.save(storeModel);
		return storeModel;
	}

	@Override
	public void removeStore(long id) {
//		storeRepository.delete(id);
		storeRepository.LogicDel(id);
	}

	@Override
	public StoreModel findStore(long id) {
		return storeRepository.findOne(id);
	}

	@Override
	public List<StoreModel> storeList(JsonObject params) {
		return Lists.newArrayList(storeRepository.findAll());
	}

	@Override
	public Page<StoreModel> storePage(JsonObject params, Pageable pageable) {
		//// TODO: 2016/8/19 params
		return storeRepository.findAll(pageable);
	}

	@Override
	public Page<StoreModel> storePageByCompany(long companyId, Pageable pageable) {
		return storeRepository.findByComapnyId(companyId,pageable);
	}
}
